package minsat;

import java.util.HashSet;
import java.util.Set;

/**
 * A vertex is a single node in a graph which tracks its neighbors using a hash set of their
 * indices. Vertices are sorted by degree by default.
 */
public class Vertex implements Comparable<Vertex>
{
  private Set<Integer> adjacent = new HashSet<Integer>();
  private double weight;
  private int index;

  public Vertex(int index, double weight)
  {
    this.index = index;
    this.weight = weight;
  }

  public void addEdge(int target)
  {
    adjacent.add(target);
  }

  public boolean removeEdge(int target)
  {
    return adjacent.remove(new Integer(target));
  }

  public boolean hasEdge(int target)
  {
    return adjacent.contains(target);
  }

  public int getDegree()
  {
    return adjacent.size();
  }

  public Set<Integer> getNeighbors()
  {
    return adjacent;
  }

  public double getWeight()
  {
    return weight;
  }

  public void decreaseWeight(double delta)
  {
    weight -= delta;
  }

  public int getIndex()
  {
    return index;
  }

  @Override
  public int compareTo(Vertex o)
  {
    if (getDegree() != o.getDegree())
      return getDegree() - o.getDegree();
    else
      return (weight >= o.getWeight()) ? 1 : -1;
  }

  public String toString()
  {
    String ret = index + ": (" + weight + ") ";
    for (Integer i : adjacent)
      ret += i + ", ";
    return ret;
  }
}
